package com.example.dps;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;



public class SettingsActivity extends PreferenceActivity {
	
	String tag = "SettingsActivity";
	
	AccountDBHelper accountDBHelper;
	DBHelper dbHelper;
	HistoryHelper historyHelper;
	
    final int CLEAR_ACCOUNT_DIALOG = 1;
    
    int selectedAccountID = -1;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);      
        
        setTheme(R.style.PrefsTheme);
        
        this.getListView().setBackgroundDrawable(Utility.returnAdjustedBackgroundDrawable(this));

        
        addPreferencesFromResource(R.xml.preferences);       
        
        
        accountDBHelper = new AccountDBHelper(this);
        dbHelper = new DBHelper(this);
        historyHelper = new HistoryHelper(this);
        
        this.setAccountEntries();
        
        
        ListPreference changeAccountInfoPref = (ListPreference) this.getPreferenceScreen().findPreference("changeAccountInfoPref");
        
        changeAccountInfoPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

			@Override
			public boolean onPreferenceChange(Preference pref, Object newValue) {

				//get the id of teh account
				int id = SettingsActivity.this.accountDBHelper.getAccountID(newValue.toString());
				
				//if account does not exist, do nothing
				if(id < 0) return false;
				
				Intent intent = new Intent(SettingsActivity.this, EditAccountActivity.class);
				
				intent.putExtra("accountName", newValue.toString());
				intent.putExtra("editAccount", true);
				
				
				SettingsActivity.this.startActivity(intent);
				
				return false;
				
			}
        	
        });
        
        
        Preference clearAccountPref = (Preference) this.getPreferenceScreen().findPreference("clearAccountPref");
        
        
        clearAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference arg0) {

				
				SettingsActivity.this.showDialog(CLEAR_ACCOUNT_DIALOG);
				
				
				return false;
			}
        	
        });
        
        
        
//        clearAccountPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
//
//			@Override
//			public boolean onPreferenceChange(Preference pref, Object newValue) {
//				
//				
//				
////				//get the selected account name
////				String accountName = (String) newValue;
////				
////				//get the corresponding ID
////				SettingsActivity.this.selectedAccountID = SettingsActivity.this.accountDBHelper.getAccountID(accountName);
////				
////				Log.e(tag, accountName + " " + SettingsActivity.this.selectedAccountID);
////				
////				//if account does not exist, don't do anything
////				if(SettingsActivity.this.selectedAccountID < 0){
////					return false;
////				}
////				
//				//show confirmation dialog to delete
//				SettingsActivity.this.showDialog(CLEAR_ACCOUNT_DIALOG);
//				
//				
//				return false;
//			}
//        	
//        });
        
//        CheckBoxPreference enableNotificationsPref = (CheckBoxPreference) this.getPreferenceScreen().findPreference("enableNotificationsPref");
//        
//        enableNotificationsPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
//
//			@Override
//			public boolean onPreferenceChange(Preference pref, Object newValue) {
//				// TODO Auto-generated method stub
//				
//				boolean checked = ((CheckBoxPreference)pref).isChecked();
//				
//				if(checked) SettingsActivity.this.dbHelper.setLastReminded();
//				
//				return false;
//			}
//        	
//        });
//        
//        
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 0, 0, "Show current settings");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 0:
                //startActivity(new Intent(this, ShowSettingsActivity.class));
                return true;
        }
        return false;
    }
    
    
    public void setAccountEntries(){
    	
    	ListPreference changeAccountInfoPref = (ListPreference) this.getPreferenceScreen().findPreference("changeAccountInfoPref");
    	//ListPreference clearAccountPref = (ListPreference) this.getPreferenceScreen().findPreference("clearAccountPref");
    	ListPreference withdrawDownPaymentPref = (ListPreference) this.getPreferenceScreen().findPreference("withdrawDownPaymentPref");
    	

        
        Cursor accountsCursor = accountDBHelper.getAccountsAsCursorNoDPS();
        
        Log.e("setAccountEntries()", "Cursor Count = " + accountsCursor.getCount());
        
        if(accountsCursor.getCount() > 0){
        	String[] accountEntries = new String[accountsCursor.getCount()];
            
            accountsCursor.moveToFirst();
            for(int i = 0; i < accountsCursor.getCount(); i++){
            	accountsCursor.moveToPosition(i);
            	accountEntries[i] = accountsCursor.getString(accountsCursor.getColumnIndex(accountDBHelper.COLUMN_ACCOUNT_NAME));
            	
            }
            changeAccountInfoPref.setEntries(accountEntries);
            changeAccountInfoPref.setEntryValues(accountEntries);
            
//            clearAccountPref.setEntries(accountEntries);
//            clearAccountPref.setEntryValues(accountEntries);
            
            withdrawDownPaymentPref.setEntries(accountEntries);
            withdrawDownPaymentPref.setEntryValues(accountEntries);
        }
        else{
        	/** no accounts in table **/
        	String[] accountEntries = { "No accounts have been setup." };
        	
            changeAccountInfoPref.setEntries(accountEntries);
            changeAccountInfoPref.setEntryValues(accountEntries);
            
//            clearAccountPref.setEntries(accountEntries);
//            clearAccountPref.setEntryValues(accountEntries);
            
            withdrawDownPaymentPref.setEntries(accountEntries);
            withdrawDownPaymentPref.setEntryValues(accountEntries);
        	
        }
        
        accountsCursor.close();
        this.accountDBHelper.close();
        
    }
    
    

    
	@Override
	public Dialog onCreateDialog(int id) {
		
		Log.e("asdfadf", "called on create Dialog");
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
		switch(id){
		
		case CLEAR_ACCOUNT_DIALOG:
			
		    builder.setMessage("Are you sure you would like to delete all account information?")
		    		.setNegativeButton("No", null)
		    		.setPositiveButton("Yes", new DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							Log.e("settingsactivity", "clear account now!");

							SettingsActivity.this.accountDBHelper.deleteAccounts();
							SettingsActivity.this.dbHelper.deleteUser();
							SettingsActivity.this.historyHelper.clearHistory();
							
							
							//refresh activity
							SettingsActivity.this.finish();
							Intent intent = new Intent(SettingsActivity.this, SettingsActivity.class);
							SettingsActivity.this.startActivity(intent);
							
						}
		    			
		    		});
		
		}
		
	    
	    
	           
	    
	    return builder.create();
	}
	

}
    
